import {guid} from '@/utils';

export function createFormDefinition() {
  return {
    id: guid(),
    formName: '单据名称',
    formKey: undefined,
    tableName: undefined,
    fields: [],
    subForms: [],
    formMain: true,//主表
    views: [createFormView(true)],
    fieldLastIndex: 0,
    subTableIndex: 0,
    hasFlow: false,

    //下面是一些子表字段
    actions: [],
    menus: [],
    flows: [],
  };
}

export function createField() {
  return {
    id: guid(),
    label: undefined,
    fieldName: undefined,
    fieldTitleName: undefined,
    component: 'input',
    length: 255,
    format: undefined,
    max_value: undefined,
    min_value: undefined,
    placeholder: undefined,
    dict: undefined,
    customComponent: false, //是否是自定义组件
    viewComponent: false, //是否是视图中用的组件
    exts: {}, //给自定义组件用的扩展配置项
  };
}

export function createSubTable(formDefinition) {
  let index = ++formDefinition.subTableIndex;
  let res = {
    id: guid(),
    formName: '明细表' + index,
    tableName: undefined, //表名
    formKey: 'subTable_' + index,
    formMain: false,//明细表
    fields: [],
  };
  formDefinition.subForms.push(res);
  return res;
}

export function createFormView(mainView = false) {
  return {
    id: guid(),
    html: '',
    templateConfigs: [],
    pageWidth: 960,
    viewName: mainView ? '主视图' : '新建视图',
    mainView,
    components: [],
  };
}

export function createDefaultAction(formDefinition, actionName, actionType, system) {
  let res = {actionName, actionType, system, id: guid()};
  let {fields, subForms} = formDefinition, fieldActions = [];
  for (let i = 0; i < fields.length; i++) {
    fieldActions.push(createFieldAction(actionType, fields[i], formDefinition.id));
  }

  (subForms || []).forEach(s => {
    let fields = s.fields || [];
    for (let i = 0; i < fields.length; i++) {
      fieldActions.push(createFieldAction(actionType, fields[i], s.id));
    }
  });

  res.fieldActions = fieldActions;
  res.subFormActions = createSubFormAction(formDefinition, actionType);
  return res;
}

export function createFieldAction(actionType, field, formId) {
  let auth = actionType === 'add' ? 'edit' : 'browse';
  return {
    auth,
    formId,
    fieldId: field.id,
    required: false,
    id: guid(),
    defaultValueType: 'manual', // manual 手动设置, system 系统变量
    defaultValue: undefined,
    defaultTitle: undefined
  };
}

export function createFormMenu() {
  return {
    id: guid(),
    showFields: undefined,
    orderFields: undefined,
    searchFields: undefined,
    searchQuerys: undefined,
    menuName: undefined,
    remark: undefined
  };
}

export function createSubFormAction(formDefinition, actionType) {
  return (formDefinition && formDefinition.subForms || []).map(subForm => ({
    id: guid(),
    subFormId: subForm.id,
    addRow: 'add' === actionType,
    removeRow: 'add' === actionType,
    copyRow: 'add' === actionType,
    clearRow: 'add' === actionType,
    moveUp: 'add' === actionType,
    moveDown: 'add' === actionType,
  }));
}

export function createMenuSearchQuery() {
  return {
    id: guid(),
    formId: undefined,
    fieldId: undefined,
    fieldValue: undefined,
    fieldTitle: undefined,
    fieldValueType: undefined,
    searchCondition: undefined,
    formMenuId: undefined,
    prefix: undefined,
    suffix: undefined,
    joinType: 'and'
  };
}

export function createFormDefinitionVO(formDefinition) {
  let data = JSON.parse(JSON.stringify(formDefinition));
  resolveFieldsVO(data.fields || []);
  resolveViewsVO(data.views || []);
  resolveSubFormVo(data.subForms || []);
  return data;
}

function resolveSubFormVo(subForms) {
  subForms.forEach(subForm => {
    let fields = subForm.fields || [];
    fields.forEach(field => {
      field.exts = JSON.stringify(field.exts);
    });
  });
}

function resolveViewsVO(views) {
  views.forEach(view => {
    view.components = view.components && JSON.stringify(view.components) || undefined;
    view.templateConfigs = view.templateConfigs && JSON.stringify(view.templateConfigs) || undefined;
  });
}

function resolveFieldsVO(fields) {
  fields.forEach(field => {
    field.exts = field.exts && JSON.stringify(field.exts) || undefined;
  });
}

export function readFormDefinitionVo(formDefinitionVo) {
  formDefinitionVo = formDefinitionVo || {};
  formDefinitionVo = JSON.parse(JSON.stringify(formDefinitionVo));
  readFieldsVO(formDefinitionVo.fields || []);
  readViewsVO(formDefinitionVo.views || []);
  readSubFormVo(formDefinitionVo.subForms || []);
  return formDefinitionVo;
}

function readSubFormVo(subForms) {
  subForms.forEach(subForm => {
    readFieldsVO(subForm.fields || []);
  });
}

function readViewsVO(views) {
  views.forEach(view => {
    view.components = view.components && JSON.parse(view.components) || [];
    view.templateConfigs = view.templateConfigs && JSON.parse(view.templateConfigs) || undefined;
  });
}

function readFieldsVO(fields) {
  fields.forEach(f => {
    f.exts = f.exts && JSON.parse(f.exts) || [];
  });
}

export function createFlow() {
  return {
    id: guid(),
    formType: undefined,
    titleFormula: undefined,
    titleFormulaText: undefined,
    beforeListeners: undefined,
    afterListeners: undefined,
    model: undefined,
    version: 0,
    enable: true,
    flowModelName: '新建流程',
  }
}